package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.DTO.StudentDTO;
import org.example.controller.request.TeaAddStudentRequest;
import org.example.entity.Choose;
import org.example.entity.User;
import org.example.service.ChooseService;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    @Autowired
    private ChooseService chooseService;

    @Autowired
    private UserService userService;

    //老师列表新增学生
    @RequestMapping("/addStudents")
    public boolean addStudent(@RequestBody TeaAddStudentRequest request){
        User user = request.getUser();
        int teacherId = request.getTeacherId();
        String teacherName = request.getTeacherName();
        System.out.println(user);
        if(user.getId() == null){
            userService.save(user);
        }
        Choose choose = new Choose();
        choose.setStudentId(user.getId());
        choose.setTeacherId(teacherId);
        choose.setTeacherName(teacherName);
        choose.setStatus("1");
        return chooseService.save(choose);
    }

    //编辑学生信息
    @PutMapping("/students/{id}")
    public boolean updateStudent(@RequestBody TeaAddStudentRequest request) {
        User user = request.getUser();

        return userService.updateById(user);
    }

    //根据老师id获取学生
    @GetMapping("/students")
    public ResponseEntity<List<StudentDTO>> getStudentsByTeacherId(@RequestParam int teacherId) {
        List<StudentDTO> students = chooseService.getStudentsByTeacherId(teacherId);

        return ResponseEntity.ok(students);
    }

    //删除学生
    @GetMapping("/delStudents")
    public ResponseEntity<Void> deleteStudent(@RequestParam int id,@RequestParam int teacherId) {
        boolean success =chooseService.delStudent(id,teacherId);

        if (success) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.status(500).build();
        }
    }
}